package com.mccken.springframework.factory.support;

import com.mccken.springframework.BeansException;
import com.mccken.springframework.factory.BeanFactory;
import com.mccken.springframework.factory.config.BeanDefinition;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-11-19 22:52
 **/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		Object bean = getSingleton(name);
		if (bean != null) {
			return bean;
		}

		BeanDefinition beanDefinition = getBeanDefinition(name);
		return createBean(name, beanDefinition, args);
	}

	@Override
	public Object getBean(String name) throws BeansException {
		return getBean(name, null);
	}

	protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException;

	protected abstract BeanDefinition getBeanDefinition(String name) throws BeansException;

}
